//
//  MPAppDelegate.m
//  MissPaparazzi
//
//  Created by Alexey Aleshkov on 18.08.12.
//  Copyright (c) 2012 DzeTech. All rights reserved.
//


#import "MPAppDelegate.h"
#import <CoreData/CoreData.h>
#import "AFDownloadRequestOperation.h"
#import "MPMainViewController.h"
#import "MPEpisode.h"
#import "MPStoreKit.h"
#import "MPStore.h"
#import "SKProductsRequest+Blocks.h"


@implementation MPAppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

NSString *const kMPIsFullScreenVideoPlayer = @"kMPIsFullScreenVideoPlayer";

- (void)dealloc
{
	[_window release];
	[_viewController release];
	[super dealloc];
}

- (NSString *)applicationDocumentsDirectory
{
	return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

- (NSManagedObjectModel *)managedObjectModel
{
	NSString *path = [[NSBundle mainBundle] pathForResource:@"MPEpisode" ofType:@"momd"];
	NSURL *momURL = [NSURL fileURLWithPath:path];
	NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
	[managedObjectModel autorelease];
	return managedObjectModel;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
	[[SKPaymentQueue defaultQueue] addTransactionObserver:[MPStoreKit sharedInstance]];
	
	self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

	UIViewController *viewController;
	if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
		NSArray *nibContent = [[NSBundle mainBundle] loadNibNamed:@"MPMainController_iPhone" owner:nil options:nil];
		viewController = [nibContent objectAtIndex:0];
		self.window.rootViewController = viewController;
	} else {
		NSArray *nibContent = [[NSBundle mainBundle] loadNibNamed:@"MPMainController_iPad" owner:nil options:nil];
		viewController = [nibContent objectAtIndex:0];
		self.window.rootViewController = viewController;
	}

	[self.window makeKeyAndVisible];
	
	return YES;
}

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
        
        BOOL isFullScreen = [[NSUserDefaults standardUserDefaults] boolForKey:kMPIsFullScreenVideoPlayer];
        if(isFullScreen)
            return UIInterfaceOrientationMaskAll;
        
        return UIInterfaceOrientationMaskPortrait;
    }
    else
        return UIInterfaceOrientationMaskAll;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
	//NSLog(@"%@", NSStringFromSelector(_cmd));

	// TODO: remove and uncomment next block
	[[MPStore sharedInstance] pauseAllDownloads];
	
//	__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
//		[[MPStore sharedInstance] pauseAllDownloads];
//		[application endBackgroundTask:backgroundTaskIdentifier];
//	}];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
	//NSLog(@"%@", NSStringFromSelector(_cmd));
	
	[[MPStore sharedInstance] resumeAllDownloads];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
}

- (void)applicationWillTerminate:(UIApplication *)application
{
}

@end
